// pages/goods_list/index.js
import { $http } from '../../request/index-api'
import regeneratorRuntime from '../../lib/runtime';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    currentIndex:1
  },
  params: {
    //商品列表ID
    cid: 0,
    //关键字
    query: "",
    pagenum: 1,
    pagesize: 10,
  },
  pageTotal: 0,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.cid){
      this.params.cid = options.cid
    }else{
      delete this.params.cid
    }
    this.params.query = options.query||''
    this._getGoodsListData()
  },
  //初始获取数据
  async _getGoodsListData (id) {
    //列表数据
    console.log(this.params);
    const listData = await $http({ url: `/goods/search`,data:this.params})
    //总页数
    this.pageTotal =  Math.ceil(listData.total / 10)
    this.setData({
      listData:[...this.data.listData,...listData.goods]
    }) 
    wx.stopPullDownRefresh()
  },
  handelCurrent(v){
    const i = v.detail
   this.setData({
    currentIndex:i
   })
  },
  //上拉加载刷新数据
  onPullDownRefresh(){
    this.params.pagenum = 1;
    this.setData({
      listData:[]
    })
    this._getGoodsListData(this.params.cid)
  },
  //下拉加载跟多数据
  onReachBottom(){
    this.params.pagenum++
    if(this.params.pagenum<=this.pageTotal){
      this._getGoodsListData(this.params.cid)
    }else{
      wx.showToast({
        title: '没有更多数据啦',
        icon: 'none',
        mask: true
      });
    }
    
  }

})